package control;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.FacilityDTO;
import model.FacilitySearchbyTypeDTO;
import business.BookingManager;

/**
 * Servlet implementation class SearchAvailableFacilitybyType
 */
@WebServlet(name = "safbtype", urlPatterns = { "/safbtype" })
public class SearchAvailableFacilitybyType extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SearchAvailableFacilitybyType() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doProcess(request, response);
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doProcess(request, response);
	}

	private void doProcess(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String type = request.getParameter("facilityType");
		String des = request.getParameter("description");
		if (type.trim().equalsIgnoreCase("")) {
			RequestDispatcher rd = request.getRequestDispatcher("/SearchPage.jsp");
			rd.forward(request, response);
		} else {
			FacilitySearchbyTypeDTO facility = new FacilitySearchbyTypeDTO();
			facility.setFacilityType(type) ;      
			
			BookingManager bm = new BookingManager();
			ArrayList<FacilityDTO> facilitylist = bm.findfacilityByCriteria(facility);
			request.setAttribute("searchlist", facilitylist);
			RequestDispatcher rd = request.getRequestDispatcher("/SearchPage.jsp");
			rd.forward(request, response);
		}

	}

}
